////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRay_GooTurret extends Pawn
                     placeable;

var SkeletalMeshComponent BaseComponent;
var SkeletalMeshComponent WeaponComponent;
var BailterRay_TurretType GooTurret;
var CylinderComponent BaseCollision;
var CylinderComponent HeadCollision;
var float ElapsedTimeSinceFiring;
var bool bReadyToFire;
var name FiringSocket;

simulated event PreBeginPlay()
{
	Super.PreBeginPlay();
    BaseCollision.SetTranslation(-(vector(BaseComponent.Rotation - rot(16384, 0 ,0)) * 40));
}


simulated function Tick(float DeltaTime)
{
    local vector FireLoc;
    local rotator FireRot;

    ElapsedTimeSinceFiring += DeltaTime;

    // Get location and direction of turret head
    WeaponComponent.GetSocketWorldLocationAndRotation(FiringSocket, FireLoc, FireRot);
    HeadCollision.SetTranslation(vector(WeaponComponent.Rotation) * 22);

    if(ElapsedTimeSinceFiring >= GooTurret.FireInterval)
    {
        Firing(FireLoc, FireRot);
        ElapsedTimeSinceFiring = 0;
    }
}

function Firing(vector FireLoc, rotator FireRot)
{

    local UTEmitter MuzzleEmitter;
    local Projectile Proj;

    // Spawns the Projectile
    Proj = Spawn(GooTurret.ProjectileClass, self,, FireLoc, FireRot);
    Proj.Init(Normal(vector(FireRot)));
    // Muzzle Flash Effect
    MuzzleEmitter = Spawn(class'UTEmitter', self,, FireLoc + (vector(FireRot) * 25), FireRot);
	MuzzleEmitter.SetTemplate(GooTurret.MuzzleFLash, true);
	MuzzleEmitter.SetDrawScale(1.8);
	PlaySound(GooTurret.FireSound, false, true, false);


}

function SetMovementPhysics()
{

}

simulated State Dying
{
	simulated function BeginState(Name PreviousStateName)
	{
		super.BeginState(PreviousStateName);
		SetPhysics(PHYS_None);
		LifeSpan = 0.01;
		SetCollision(false);
		PlayDeathEffect();
	}

    // Do nothing when dead!
	function TakeDamage(int Damage, Controller EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser){}
	simulated function Tick(float DeltaTime){}


	simulated function PlayDeathEffect()
	{
	    local UTEmitter DeathEmitter;

        DeathEmitter = Spawn(class'UTEmitter', self,, self.Location, rot(0,16384,0));
	    DeathEmitter.SetTemplate(GooTurret.DeathEffect, true);
	    DeathEmitter.SetDrawScale(2);
	    DeathEmitter.PlaySound(GooTurret.DeathSound, false, true, false);
	}
}

function TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
    // So turrets dont destory themselves
    if(InstigatedBy.bIsPlayer == false)
    {
        return;
    }

	super.TakeDamage(Damage, InstigatedBy, HitLocation, Momentum, DamageType, HitInfo, DamageCauser);

    //Play hit effects based on hitlocation
    PlaySound(GooTurret.DeathSound, true,,, HitLocation);
    WorldInfo.MyEmitterPool.SpawnEmitter(GooTurret.DeathEffect, HitLocation, Rotator(HitLocation));

}

defaultproperties
{
    //Health of turret
    Health=100

    //Clear any components from the parent class!
    Components.Empty

    //Base of Turret
    Begin Object class=SkeletalMeshComponent Name=BaseMesh
        SkeletalMesh=SkeletalMesh'BailterRayAssetPackage.Meshes.PlantTurretBase'
    End Object
    BaseComponent=BaseMesh
    Components.Add(BaseMesh)

    //Head of Turret
    Begin Object Class=SkeletalMeshComponent Name=HeadMesh
        SkeletalMesh=SkeletalMesh'BailterRayAssetPackage.Meshes.PlantTurretHead'
        Rotation=(Pitch = -16384, Yaw = 0, Roll = 0)
    End Object
    WeaponComponent=HeadMesh
    Components.Add(HeadMesh)

    //Turret Type
    Begin Object class=BailterRay_TurretType Name=Proj
	  ProjectileClass=class'BailterRayProj_Goo'
        MuzzleFlash=ParticleSystem'WP_BioRifle.Particles.P_WP_Bio_3P_MF'
     	  DeathEffect=ParticleSystem'WP_BioRifle.Particles.P_WP_Bio_Alt_Blob_POP'
	  DeathSound = SoundCue'A_Weapon_BioRifle.Weapon.A_BioRifle_FireAltImpactExplode_Cue'
	  FireSound = SoundCue'A_Character_BodyImpacts.BodyImpacts.A_Character_BodyImpact_GibLarge_Cue'
	  FireInterval=3.6
    End Object
    GooTurret=Proj

    //Collision
    Begin Object class=CylinderComponent name=CollisionBase
        CollisionRadius=15.0
        CollisionHeight=32.0
        CollideActors=True
        BlockActors=True
    End Object
    BaseCollision = CollisionBase
    Components.Add(CollisionBase)

    Begin Object class=CylinderComponent name=CollisionHead
	  CollisionRadius=14.0
	  CollisionHeight=20.0
        CollideActors=True
        BlockActors=True
    End Object
    HeadCollision = CollisionHead
    Components.Add(CollisionHead)

    //Removing Default Collision
    Begin Object Name=CollisionCylinder
        CollisionHeight=1.0
        CollisionRadius=1.0
        CollideActors=True
        BlockActors=True
    End Object
    Components.Add(CollisionCylinder)
    CollisionComponent=CollisionCylinder
    CollisionType=COLLIDE_BlockAll

    FiringSocket = FiringMuzzle

    Components.Remove(Sprite)
    Name="Default__BailterRayGooTurret"

}